package com.phy.gsjl.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.phy.gsjl.entity.Message;
import com.phy.gsjl.entity.Reply;
import com.phy.gsjl.form.MessageForm;
import com.phy.gsjl.form.MessagePlus;
import com.phy.gsjl.form.QueryForm;
import com.phy.gsjl.form.ReplyForm;
import com.phy.gsjl.service.IMessageService;
import com.phy.gsjl.service.IReplyService;

/**
 * 信息管理控制器
 * 
 * @author curry
 *
 */
@Controller
@RequestMapping(value = "/message")
public class MessageController {

	@Autowired
	private IMessageService messageServiceImpl;

	@Autowired
	private IReplyService replyServiceImpl;

	//获取日志对象
	private static Logger logger = Logger.getLogger(MessageController.class);
	
	/**
	 * 显示所有信息
	 * 
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/lists", method = RequestMethod.POST)
	public Map<String, Object> getMessages(Integer type) {
		logger.info("==>lists info of request type: " + type);
		// 调用Service层的查询方法
		List<Message> messages = messageServiceImpl.getAllMessages(type);
		List<MessagePlus> messagePluses = new ArrayList<MessagePlus>();
		if (messages.size() != 0) {
			for (Message message : messages) {
				logger.info("==>info of lists messages:" + message.toString());
				logger.info("==>info of lists messagesplus:" + transforTo(message).toString());
				messagePluses.add(transforTo(message));
			}
		}
		Map<String, Object> info = new HashMap<String, Object>();
		info.put("iTotalRecords", messages.size());// 实际的行数
		info.put("iTotalDisplayRecords", messages.size());// 显示的行数,这个要和上面写的一样
		info.put("data", messagePluses);
		return info;
	}

	/**
	 * 显示所有信息(分页）
	 * 
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public List<MessagePlus> getMessage(Integer type, Integer page) {
		// 调用Service层的查询方法
		List<Message> messages = messageServiceImpl.getMessageByPage(page, type);
		List<MessagePlus> messagePluses = new ArrayList<MessagePlus>();
		if (messages.size() != 0) {
			for (Message message : messages) {
				messagePluses.add(transforTo(message));
			}
		}
		return messagePluses;
	}

	/**
	 * 获取总的记录数
	 * 
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/records", method = RequestMethod.GET)
	public Integer getTotalRecords(Integer type) {
		logger.info("==>records info of type: " + type);
		// 调用Service层的查询方法
		return messageServiceImpl.getTotalRecords(type);
	}

	/**
	 * 根据内容模糊查询
	 * 
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/indistinct", method = RequestMethod.POST)
	public List<MessagePlus> getMessageIndistinct(Integer type, String indistinct) {
		logger.info("==>indistinct info of indistinct:" + indistinct);
		// 调用Service层的查询方法
		List<Message> messages = messageServiceImpl.getMessageIndistinct(type, indistinct);
		List<MessagePlus> messagePluses = new ArrayList<MessagePlus>();
		if (messages.size() != 0) {
			for (Message message : messages) {
				messagePluses.add(transforTo(message));
			}
		}
		return messagePluses;
	}

	/**
	 * 根据内容和起始时间进行模糊查询
	 * @param queryForm
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/indistincts", method = RequestMethod.POST)
	public List<MessagePlus> getMessageByDetailsAndTime(Integer type, String indistinct, String begin, String end){
		Timestamp beginTime = Timestamp.valueOf(begin); //转化成日期格式
		Timestamp endTime = Timestamp.valueOf(end);
		QueryForm queryForm = new QueryForm(type, indistinct, beginTime, endTime);
		logger.info("==>indistincts info of queryForm:" + queryForm.toString());
		// 调用Service层的查询方法
		List<Message> messages = messageServiceImpl.getMessageByDetailsAndTime(queryForm);
		List<MessagePlus> messagePluses = new ArrayList<MessagePlus>();
		if (messages.size() != 0) {
			for (Message message : messages) {
				messagePluses.add(transforTo(message));
			}
		}
		return messagePluses;
	}
	
	/**
	 * 查看详情
	 * 
	 * @param m_id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/details")
	public Map<String, Object> getDetails(@RequestParam(value = "m_id", required = false) Integer m_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		Message message = messageServiceImpl.getMessageById(m_id);
		MessagePlus messagePlus = transforTo(message); //时间转换
		List<Reply> replies = replyServiceImpl.getAllReplys(m_id);
		// 将信息详情和回复信息以json的格式返回
		map.put("message", messagePlus);
		map.put("replies", replies);
		logger.info("==>details info of the m_id:" + m_id);
		logger.info("==>details info of message time: " + message.getM_time());
		return map;
	}

	/**
	 * 新增信息
	 * 
	 * @param message
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addMessage")
	public String addMessage(MessageForm messageForm) {
		logger.info("==>addMessage info of the messageForm:" + messageForm.toString());
		// 由MessageForm转化成Message
		Message message = new Message(messageForm.getType(), messageForm.getUsername(), messageForm.getM_details());
		// 调用Service层的查询方法，返回添加成功与否标识
		boolean isAdded = messageServiceImpl.addMessage(message);
		if (isAdded) {
			return "AddMessageSuccess" + message.getType();
		} else {
			return "AddMessageFailed";
		}
	}

	/**
	 * 添加回复
	 * 
	 * @param reply
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addReply")
	public String addReply(ReplyForm replyForm) {
		logger.info("==>addReply info of the replyForm:" + replyForm.toString());
		// 由ReplyForm转化成Reply
		Reply reply = new Reply(replyForm.getM_id(), replyForm.getUsername(), replyForm.getR_details());
		// 调用Service层的查询方法，返回更新成功与否标识
		boolean isAddReply = replyServiceImpl.addReply(reply);
		// 更新状态信息
		Message message = messageServiceImpl.getMessageById(reply.getM_id());
		message.setStatus(1); // 设置为已回复
		// 调用Service层的查询方法，返回更新成功与否标识
		boolean isChangeStatus = messageServiceImpl.updateMessageStatus(message);
		if (isAddReply && isChangeStatus) {
			return "AddReplySuccess" + message.getType();
		} else {
			return "AddReplyFailed";
		}
	}
	
	/**
	 * 转化方法
	 * @param message
	 * @return
	 */
	public MessagePlus transforTo (Message message) {
		MessagePlus messagePlus = new MessagePlus();
		messagePlus.setM_id(message.getM_id());
		messagePlus.setM_number(message.getM_number());
		messagePlus.setM_time(message.getM_time().toString());
		messagePlus.setType(message.getType());
		messagePlus.setM_details(message.getM_details());
		messagePlus.setUsername(message.getUsername());
		messagePlus.setStatus(message.getStatus());
		return messagePlus;
	}
}
